/*
 * Created on 23.02.2004
 * Local Projects Database
 * Mihai Postelnicu (mihai@ro-gateway.org)
 * (c) 2003 by eRomania Gateway
 */
package ro.gateway.aida.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.obj.Person;
import ro.gateway.aida.obj.db.PersonDB;
import ro.gateway.aida.obj.searchrows.PersonSelectorRow;
import ro.gateway.aida.struts.MultiTilesAction;
import ro.gateway.aida.struts.form.PersonSelectorForm;
import ro.gateway.aida.usr.User;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.utils.PageToken;
import ro.xblue.translator.TranslatorUtils;


/**
 * @author Mihai Postelnicu<p>
 *  Searches & selects person objects
 *  e-mail (mihai@ro-gateway.org)<br>
 * (c) 2003 by eRomania Gateway<p>
 * @version 2.0
 */
public class PersonSelectorAction extends MultiTilesAction {
    private PersonDB pdb;

    public ActionForward modePrepare(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        User user = UserUtils.getLogged(request, response);

        /*if (user == null) {
            return mapping.findForward("plslogin");
        }*/

        if (pdb == null) {
            pdb = PersonDB.getManager(token);
        }


        return modeSelect(mapping, form, request, response);
    }

    public ActionForward modeSave(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        HttpSession session = request.getSession();

        PersonSelectorForm f = (PersonSelectorForm) form;
        Person p = pdb.getByID(f.getSelectedPerson());

        if (p == null)
            session.removeAttribute(Person.SELECTED);
        else
            session.setAttribute(Person.SELECTED, p);

        request.setAttribute("save", "save");

        return mapping.getInputForward();
    }

    public ActionForward modeSearch(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        PersonSelectorForm eaf = (PersonSelectorForm) form;
        HttpSession session = request.getSession();
        String lang = TranslatorUtils.getCurrentLanguage(request,
                getServlet().getServletContext()).getLanguage();
        List l = pdb.selectorSearch(eaf.getName(), eaf.getKeyword(), lang);
        session.setAttribute(PersonSelectorRow.LIST, l);

        return modeView(mapping, form, request, response);
    }

    public ActionForward modeView(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        HttpSession session = request.getSession();
        List l = (List) session.getAttribute(PersonSelectorRow.LIST);
        PageToken.tokenizeList( request,  sc, l, "PersSearchResults");    

        return mapping.getInputForward();
    }

    public ActionForward modeSelect(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        if (request.getParameter("save") != null) {
            return modeSave(mapping, form, request, response);
        }

        if (request.getParameter("search") != null) {
            return modeSearch(mapping, form, request, response);
        }

        if (request.getParameter("cancel") != null) {
            HttpSession session = request.getSession();
            request.setAttribute("cancel", "cancel");
            session.removeAttribute(PersonSelectorRow.LIST);

            return mapping.getInputForward();
        }

        return modeView(mapping, form, request, response);
    }
}
